﻿using Humanizer;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Outpatient.Doman.Hospitalization;
using Outpatient.ErrorCode;
using OutpatientApi.DTO;
using OutpatientApi.Methodimplementation.IServices;

namespace OutpatientApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PharmacymanagementController : ControllerBase
    {
        private readonly ILogger<PharmacymanagementController> logger;
        private readonly IPharmacymanagementServices pharmacymanagementServices;

        public PharmacymanagementController(ILogger<PharmacymanagementController> logger,IPharmacymanagementServices pharmacymanagementServices)
        {
            this.logger = logger;
            this.pharmacymanagementServices = pharmacymanagementServices;
        }


        /// <summary>
        /// 未打单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<PharmacyDto>>> GetPharmacymanagement([FromQuery]GetPharmacymanagementDto dto)
        {
            try
            {
                return await pharmacymanagementServices.GetPharmacymanagement(dto);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 已打单未发药
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<PharmacyDto>>> Themedicinehasnotbeenissued([FromQuery]GetPharmacymanagementDto dto)
        {
            try
            {
                return await pharmacymanagementServices.Themedicinehasnotbeenissued(dto);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 查找已发药品信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<PharmacyDto>>> GeuData()
        {
            try
            {
                return await pharmacymanagementServices.GeuData();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 药品查找方法
        /// </summary>
        /// <param name="dto">查询条件</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MedicineDto>>> SearchMedicine([FromQuery]SearchMedicineDto dto)
        {
            try
            {
                return await pharmacymanagementServices.SearchMedicine(dto);

            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 药品出库方法
        /// </summary>
        /// <param name="medicineId">药品ID</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult<object>> MedicineOutbound(int medicineId)
        {
            try
            {
                return await pharmacymanagementServices.MedicineOutbound(medicineId);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 药品入库
        /// </summary>
        /// <param name="dto">药品信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Create_medicines(Create_medicinesDto dto)
        {
            try
            {
                return await pharmacymanagementServices.Create_medicines(dto);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "药品入库失败");
                return new ApiResult { msg = "药品入库失败", code = ApiREunm.Fail };
            }
        }

        /// <summary>
        /// 修改药品信息
        /// </summary>
        /// <param name="dto">修改的药品信息</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> UpdateMedicine(UpdateMedicineDto dto)
        {
            try
            {
                return await pharmacymanagementServices.UpdateMedicine(dto);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "修改药品信息失败");
                return new ApiResult { msg = "修改药品信息失败", code = ApiREunm.Fail };
            }
        }

        /// <summary>
        /// 实现发药功能
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> Dispensing_medicine(int MedicalCardRegistrationId)
        {
            try
            {
                return await pharmacymanagementServices.Dispensing_medicine(MedicalCardRegistrationId);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 实现打单功能
        /// </summary>
        /// <param name="MedicalCardRegistrationId">医疗卡注册ID</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> PrintPrescription(int MedicalCardRegistrationId)
        {
            try
            {
                return await pharmacymanagementServices.PrintPrescription(MedicalCardRegistrationId);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "打单操作失败");
                return new ApiResult { msg = "打单操作失败", code = ApiREunm.Fail };
            }
        }


    }
}
